#import "ALAddEventSheetController.h"


#pragma mark Constants

static const CGFloat ALTemplateTableCellVerticalPadding = 8.0f;


#pragma mark -
@interface ALAddEventSheetController (ALAddEventSheetControllerPrivateMethods)
-(void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)context;
@end


#pragma mark -
@implementation ALAddEventSheetController

@synthesize templatesArrayController;
//@synthesize eventColumnView = eventColumnView_;
//@synthesize sourceListColumnView = sourceListColumnView_;


-(void)displaySheetInWindow:(NSWindow *)parentWindow {
	[NSApp beginSheet:[self window] modalForWindow:parentWindow modalDelegate:self didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) contextInfo:NULL];
}

-(void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)context {
	[sheet orderOut:self];
}


#pragma mark -
#pragma mark Accessors

-(NSArray *)templateSortDescriptors {
	return [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:ALLabelKey ascending:YES]];
}

-(NSArray *)selectedTemplates {
	return [[self templatesArrayController] selectedObjects];
}


#pragma mark Templates table delegate

-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
	NSTableColumn *tableColumn = [[tableView tableColumns] objectAtIndex:0];
	NSCell *tableCell = [tableView preparedCellAtColumn:0 row:row];
	NSRect cellBounds = NSMakeRect(0, 0, [tableColumn width], CGFLOAT_MAX);
	NSSize minSize = [tableCell cellSizeForBounds:cellBounds];
	return minSize.height + ALTemplateTableCellVerticalPadding;
}

-(NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation {
	return nil;
}


#pragma mark -
#pragma mark Actions

-(IBAction)cancelSheet:(id)sender {
	[NSApp endSheet:[self window]];
}

-(IBAction)addSelectedTemplates:(id)sender {
	[NSApp endSheet:[self window]];
	[NSApp sendAction:@selector(addEventSheetEntries:) to:nil from:self];
}

@end
